package top.ljlgithub.service.impl;

import top.ljlgithub.dao.AdminDao;
import top.ljlgithub.domain.PageBean;
import top.ljlgithub.domain.PageConditionSql;
import top.ljlgithub.domain.PageSql;
import top.ljlgithub.domain.User;
import top.ljlgithub.service.AdminService;
import top.ljlgithub.vo.LoginParam;
import top.ljlgithub.vo.PageParam;

import javax.crypto.interfaces.PBEKey;
import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/3/21 8:31
 * @Version 1.0
 **/
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao;

    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    public int checkAdmin(LoginParam admin) {
        return adminDao.checkAdmin(admin);
    }

    @Override
    public int addUser(User user) {
        return adminDao.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return adminDao.updateUser(user);
    }

    @Override
    public User findUserById(int id) {
        return adminDao.findUserById(id);
    }

    @Override
    public int removeUser(int id) {
        return adminDao.deleteUserById(id);
    }

    @Override
    public int removeUsers(int[] ids) {
        int nums = 0;
        //在前端中设置不安全，用户可能改变前端js代码或者直接在url上访问造成空指针异常
        //在前端和后端同时校验，提高程序的健壮性
        if (ids != null && ids.length > 0) {
            for (nums = 0; nums < ids.length; nums++) {
                adminDao.deleteUserById(ids[nums]);
            }
        }
        return nums;
    }

    @Override
    public PageBean<User> getUserByPage(PageParam pageParam) {
        //1.获取总记录数
        int totalCount = adminDao.findTotalCount();
        //2.总页数
        //获取每页的条数
        int rows = pageParam.getRows();
        int pageCount = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
        //3.获取页面的用户
        //获取参数
        int startIndex = (pageParam.getCurrentPage() - 1) * pageParam.getRows();
        PageSql pageSql = new PageSql();
        pageSql.setRows(pageParam.getRows());
        pageSql.setStartIndex(startIndex);
        List<User> users = adminDao.findUserByPage(pageSql);
        //4.封装到对象去
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setList(users);
        userPageBean.setTotalCount(totalCount);
        userPageBean.setTotalPage(pageCount);
        return userPageBean;
    }

    @Override
    public PageBean<User> getUserByPage(PageConditionSql pageConditionSql) {
        //1.获取总记录数
        int totalCount = adminDao.findTotalCountByQuery(pageConditionSql.getCondition());
        //2.总页数
        //获取每页的条数
        int rows = pageConditionSql.getRows();
        int pageCount = totalCount % rows == 0 ? totalCount / rows : totalCount / rows + 1;
        //3.获取页面的用户
        //获取参数
        int startIndex = (pageConditionSql.getCurrentPage() - 1) * pageConditionSql.getRows();
        pageConditionSql.setStartIndex(startIndex);
        List<User> users = adminDao.findUserByPageAndCondition(pageConditionSql);
        //4.封装到对象去
        PageBean<User> userPageBean = new PageBean<>();
        userPageBean.setList(users);
        userPageBean.setTotalCount(totalCount);
        userPageBean.setTotalPage(pageCount);
        return userPageBean;
    }
}
